package com.woniuxy.yoga.progress.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.OrderState;
import com.woniuxy.yoga.commons.core.statedata.ProgressState;
import com.woniuxy.yoga.commons.core.statedata.UserState;
import com.woniuxy.yoga.progress.client.OrderClient;
import com.woniuxy.yoga.progress.client.UserClient;
import com.woniuxy.yoga.progress.dao.model.TeamVacate;
import com.woniuxy.yoga.progress.dao.mapper.TeamVacateMapper;
import com.woniuxy.yoga.progress.model.dto.OrderInfoDto;
import com.woniuxy.yoga.progress.model.dto.UserInfoDto;
import com.woniuxy.yoga.progress.model.exception.ProgressException;
import com.woniuxy.yoga.progress.model.exception.ProgressExceptionCode;
import com.woniuxy.yoga.progress.model.param.SelectVacateParam;
import com.woniuxy.yoga.progress.model.param.VacateParam;
import com.woniuxy.yoga.progress.service.TeamVacateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 团课请假记录表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class TeamVacateServiceImpl extends ServiceImpl<TeamVacateMapper, TeamVacate> implements TeamVacateService {

    @Autowired
    private TeamVacateMapper teamVacateMapper;

    @Autowired
    private UserClient userClient;

    @Autowired
    private OrderClient orderClient;

    /**
     * 用户请假申请
     * @param param 请假申请参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void vacate(VacateParam param) {
        //查询用户
        Result<UserInfoDto> userInfoDtoResult = userClient.getByUserId(param.getUserId());
        if (userInfoDtoResult.getCode()!=200) throw new ProgressException(userInfoDtoResult.getCode(),userInfoDtoResult.getMessage());
        UserInfoDto userInfoDto = userInfoDtoResult.getData();
        //判断用户状态
        if (!Objects.equals(userInfoDto.getUserState(), UserState.USER_ACCOUNT_STATE_NORMAL))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_USER_STATE_WRONG);
        //查询订单
        Result<OrderInfoDto> orderInfoDtoResult = orderClient.getByOrderNum(param.getOrderNum());
        if (orderInfoDtoResult.getCode()!=200)
            throw new ProgressException(orderInfoDtoResult.getCode(),orderInfoDtoResult.getMessage());
        //判断订单状态
        OrderInfoDto orderInfoDto = orderInfoDtoResult.getData();
        if (!Objects.equals(orderInfoDto.getOrderState(), OrderState.ORDER_STATE_PAYED))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_ORDER_STATE_WRONG);
        //创建请假申请对象
        TeamVacate teamVacate = new TeamVacate();
        teamVacate.setTeamCourseId(param.getTeamCourseId());
        teamVacate.setUserId(param.getUserId());
        teamVacate.setOrderNum(param.getOrderNum());
        teamVacate.setTeamProductId(orderInfoDto.getProductId());
        teamVacate.setVacateTime(param.getVacateTime());
        teamVacate.setVacateReason(param.getVacateReason());
        teamVacate.setVacateCount(param.getVacateCount());
        teamVacate.setVacateState(ProgressState.VACATE_STATE_NO);
        teamVacateMapper.insert(teamVacate);
    }

    /**
     * 同意请假
     * @param vacateId 请假表id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void agreeVacate(Integer vacateId) {
        //查询
        TeamVacate teamVacate = teamVacateMapper.selectById(vacateId);
        if (teamVacate == null) throw  new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_TEA_VACATE_NOT_EXIST);
        //验证
        if (!Objects.equals(teamVacate.getVacateState(), ProgressState.VACATE_STATE_NO))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_TEA_VACATE_STATE_WRONG);
        //修改请假状态
        teamVacate.setVacateState(ProgressState.VACATE_STATE_YES);
        teamVacateMapper.updateById(teamVacate);
    }

    /**
     * 拒绝请假
     * @param vacateId 请假表id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rejectVacate(Integer vacateId) {
        //查询
        TeamVacate teamVacate = teamVacateMapper.selectById(vacateId);
        if (teamVacate == null) throw  new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_TEA_VACATE_NOT_EXIST);
        //验证
        if (!Objects.equals(teamVacate.getVacateState(), ProgressState.VACATE_STATE_NO))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_TEA_VACATE_STATE_WRONG);
        //修改请假状态
        teamVacate.setVacateState(ProgressState.VACATE_STATE_REJECT);
        teamVacateMapper.updateById(teamVacate);
    }

    /**
     * 分页查询请假申请
     * @param param 参数
     * @return pageInfo
     */
    @Override
    public PageInfo selectVacates(SelectVacateParam param) {
        //条件查询
        QueryWrapper<TeamVacate> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getVacateState()))
            wrapper.eq("vacate_state",param.getVacateState());
        if (ObjectUtil.isNotEmpty(param.getUserId()))
            wrapper.eq("user_id",param.getUserId());
        wrapper.orderByDesc("vacate_time");
        //创建分页对象
        Page<TeamVacate> page = new Page<>(param.getPageNum(), param.getPageSize());
        teamVacateMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(page.getRecords());
        return pageInfo;
    }
}
